﻿/********************************************************************************/
// <copyright file="MemberRepository.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="24/12/2012 4:20:59 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Model.DTO;

namespace JLL.China.Infrastructure.Repositories.Member
{
    public class MemberRepository : SQLRepositoryBase<Model.Member, int>, IMemberRepository
    {

        public override IList<Model.Member> FindAll()
        {
            var member = DataContext.Members.OrderBy(x => x.UserName).ToList();
            if (member != null)
            {
                var allMembers = new List<Model.Member>();
                foreach (var m in member)
                {
                    allMembers.Add(MemberFactory.FromLINQObject(m));
                }
                return allMembers;
            }
            else
            {
                return new List<Model.Member>();
            }
        }

        public override Model.Member FindBy(int key)
        {
            var member = DataContext.Members.SingleOrDefault(x => key == x.Member_ID);
            if (member == null)
                return null;
            else
                return MemberFactory.FromLINQObject(member); 
        }

        protected override void PersistDeletedItem(Model.Member item)
        {
            var member = DataContext.Members.SingleOrDefault(x => x.Member_ID == item.ID);
            if (member != null)
                DataContext.Members.DeleteOnSubmit(member);
        }

        protected override void PersistNewItem(Model.Member item)
        {
            DataContext.Members.InsertOnSubmit(MemberFactory.ToLINQObject(item));
        }

        protected override void PersistUpdatedItem(Model.Member item)
        {
            var existMem = DataContext.Members.SingleOrDefault(x => x.Member_ID == item.ID);
            if (existMem != null)
            {
                var member = item;
                existMem.UserName = member.UserName;
                existMem.Password = member.Password;
                existMem.LastName = member.LastName;
                existMem.FirstName = member.FirstName;
                existMem.Email = member.Email;
                existMem.Mobile = member.Mobile;
                existMem.GroupFID = member.Group.ID;
            }
            else
            {
                throw new Exception("Member not found to update");
            }
        }

        
        public Model.Member FindByUsername(string username)
        {
            var member = DataContext.Members.SingleOrDefault(x => x.UserName == username);
            if (member != null)
                return MemberFactory.FromLINQObject(member);
            else 
                return null;
        }

        public IList<Model.Member> Search(MemberSearchCriteria searchCriteria)
        {
            var items = from m in DataContext.Members
                        select m;

            if (!searchCriteria.Descending)
            {
                switch (searchCriteria.OrderBy)
                {
                    case MemberSearchOrder.Email:
                        items = items.OrderBy(x => x.Email);
                        break;
                    case MemberSearchOrder.FirstName:
                        items = items.OrderBy(x => x.FirstName);
                        break;
                    case MemberSearchOrder.LastName:
                        items = items.OrderBy(x => x.LastName);
                        break;
                    case MemberSearchOrder.Mobile:
                        items = items.OrderBy(x => x.Mobile);
                        break;
                    default:
                        items = items.OrderBy(x => x.UserName);
                        break;
                }
            }
            else
            {
                switch (searchCriteria.OrderBy)
                {

                    case MemberSearchOrder.Email:
                        items = items.OrderByDescending(x => x.Email);
                        break;
                    case MemberSearchOrder.FirstName:
                        items = items.OrderByDescending(x => x.FirstName);
                        break;
                    case MemberSearchOrder.LastName:
                        items = items.OrderByDescending(x => x.LastName);
                        break;
                    case MemberSearchOrder.Mobile:
                        items = items.OrderByDescending(x => x.Mobile);
                        break;
                    default:
                        items = items.OrderByDescending(x => x.UserName);
                        break;
                }
            }
            var list = items.Skip(searchCriteria.PageIndex * searchCriteria.Count).Take(searchCriteria.Count).ToList();
            var result = new List<Model.Member>();
            foreach (var n in list)
            {
                result.Add(MemberFactory.FromLINQObject(n));
            }
            return result;
        }

        public int CountMembers(MemberSearchCriteria searchCriteria)
        {
            throw new NotImplementedException();
        }

        public bool ValidateLogin(string username, string password)
        {
            return DataContext.Members.Any(x => x.UserName == username && x.Password == password);
        }
    }
}
